#include"http.hpp"

#define WWWROOT "./wwwroot/"

std::string RequestSrt(const wyl::HttpRequest & req)
{
  std::string str ; 
  str += req._method + " " + req._path + " " + req._version + " " + "\n";
  for(auto& e : req._headers)
  {
    str += e.first + ": " + e.second + "\n";
  }
  for(auto& e : req._params)
  {
    str += e.first + ": " + e.second + "\n";
  }
  str += "\n";
  str += req._body; 

  return str;
}

void Hello(const wyl::HttpRequest& req, wyl::HttpResponse* resp)
{
  sleep(15);
  resp->SetContext(RequestSrt(req),"text/plain");
}

void Login(const wyl::HttpRequest& req, wyl::HttpResponse* resp)
{
  resp->SetContext(RequestSrt(req),"text/plain");
}
void Put(const wyl::HttpRequest& req, wyl::HttpResponse* resp)
{
  resp->SetContext(RequestSrt(req),"text/plain");
}
void Delete(const wyl::HttpRequest& req, wyl::HttpResponse* resp)
{
  resp->SetContext(RequestSrt(req),"text/plain");
}

int main()
{
  wyl::HttpServer server(8081);
  server.SetBaseDir(WWWROOT);
  server.SetThreadCount(3);
  server.AddGet("/hello",Hello);
  server.AddPost("/login",Login);
  server.AddPut("/1234.txt",Put);
  server.AddDelete("/1234.txt",Delete);
  server.Listen();
  

  return 0;
}
